package com.example.a12.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.a12.R;
import com.example.a12.activity.NovelDetailActivity;
import com.example.a12.adapter.NovelAdapter;
import com.example.a12.model.Novel;

import java.util.ArrayList;
import java.util.List;

public class NovelFragment extends Fragment {

    private List<Novel> novelList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initNovels();
    }

    private void initNovels() {
        // 添加小说数据，包括标题、简介、图片资源ID和URL
        novelList.add(new Novel("三体",
                "刘慈欣创作的长篇科幻小说系列，讲述了人类文明与外星文明的交流与对抗。",
                R.drawable.santi,
                "https://book.douban.com/subject/3066477/"));

        novelList.add(new Novel("活着",
                "余华的代表作之一，讲述了在大时代背景下，主人公福贵的人生和家庭不断经受着苦难。",
                R.drawable.huozhe,
                "https://book.douban.com/subject/4913064/"));

        novelList.add(new Novel("围城",
                "钱钟书所著的长篇小说，是中国现代文学史上一部风格独特的讽刺小说。",
                R.drawable.weicheng,
                "https://book.douban.com/subject/27070488/"));

        novelList.add(new Novel("平凡的世界",
                "路遥创作的长篇小说，以中国70年代中期到80年代中期10年间为背景。",
                R.drawable.pingfan,
                "https://book.douban.com/subject/10517238/"));

        novelList.add(new Novel("解忧杂货店",
                "东野圭吾创作的长篇小说，讲述了人们在面临人生困境时，通过写信向解忧杂货店寻求帮助的故事。",
                R.drawable.jieyou,
                "https://book.douban.com/subject/25862578/"));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_novel, container, false);
        ListView listView = view.findViewById(R.id.novel_list);

        // 使用自定义适配器
        NovelAdapter adapter = new NovelAdapter(requireContext(), R.layout.item_novel, novelList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position >= 0 && position < novelList.size()) {
                    Novel novel = novelList.get(position);
                    Intent intent = new Intent(requireContext(), NovelDetailActivity.class);
                    intent.putExtra("title", novel.getTitle());
                    intent.putExtra("summary", novel.getSummary());
                    intent.putExtra("url", novel.getUrl());
                    startActivity(intent);
                } else {
                    Toast.makeText(requireContext(), "无效的小说选择", Toast.LENGTH_SHORT).show();
                }
            }
        });

        return view;
    }
}